import 'package:flutter/material.dart';

/// 通用输入框样式
InputDecoration buildInputDecoration({
  required BuildContext context,
  required String hintText,
  Widget? prefixIcon,
}) {
  return InputDecoration(
    hintText: hintText,
    hintStyle: const TextStyle(
      color: Color(0xFF999999),
      fontSize: 15,
      height: 1.2,
    ),
    filled: true,
    fillColor: Colors.white,
    border: OutlineInputBorder(
      borderSide: const BorderSide(color: Color(0xFFE5E7EB)),
      borderRadius: BorderRadius.circular(8),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Theme.of(context).primaryColor, width: 1.5),
      borderRadius: BorderRadius.circular(8),
    ),
    errorBorder: OutlineInputBorder(
      borderSide: const BorderSide(color: Colors.red, width: 1.5),
      borderRadius: BorderRadius.circular(8),
    ),
    contentPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 14),
    errorStyle: const TextStyle(fontSize: 12, height: 1, color: Colors.red),
    prefixIcon: prefixIcon,
    prefixIconConstraints: const BoxConstraints(minWidth: 40),
    isDense: true,
  );
}

Widget buildFormLabel(String text, {bool required = false}) {
  return RichText(
    text: TextSpan(
      text: text,
      style: const TextStyle(
        color: Color(0xFF1D2129),
        fontSize: 16,
        fontWeight: FontWeight.w500,
        fontFamily: 'CustomFont',
        height: 1.2,
      ),
      children: [
        if (required)
          const TextSpan(
            text: ' *',
            style: TextStyle(color: Colors.red, fontSize: 16),
          ),
      ],
    ),
  );
}

/// 表单底部按钮组组件
Widget buildFormButtonGroup({
  required BuildContext context,
  required VoidCallback onConfirm,
}) {
  return Row(
    children: [
      Expanded(child: _buildCancelButton(context)),
      const SizedBox(width: 16),
      Expanded(child: _buildSubmitButton(context, onConfirm)),
    ],
  );
}

/// 取消按钮
Widget _buildCancelButton(BuildContext context) {
  return ElevatedButton(
    onPressed: () => Navigator.pop(context), // 直接使用传入的context返回
    style: ElevatedButton.styleFrom(
      backgroundColor: Colors.white,
      foregroundColor: const Color(0xFF4E5969),
      side: const BorderSide(color: Color(0xFFDCDFE6)),
      padding: const EdgeInsets.symmetric(vertical: 15),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      elevation: 0,
    ),
    child: const Text('取消'),
  );
}

/// 提交按钮
Widget _buildSubmitButton(BuildContext context, VoidCallback onConfirm) {
  return ElevatedButton(
    onPressed: onConfirm,
    style: ElevatedButton.styleFrom(
      backgroundColor: Theme.of(context).primaryColor, // 使用传入的context获取主题
      padding: const EdgeInsets.symmetric(vertical: 15),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      elevation: 0,
    ),
    child: const Text('提交', style: TextStyle(color: Colors.white)),
  );
}
